#encoding=utf-8
import logging.config

from .k8sPaiLibrary.maintainlib import add as k8s_add
from .k8sPaiLibrary.maintainlib import remove as k8s_remove

logger = logging.getLogger(__name__)

# True : continue
# False: exit
class MachineCmd(object):
    def process_args(self, args):
        kubernetes_config = args["kubernetes_config"]
        node_list = args["node_list"]

        for host in node_list["machine-list"]:
            if "nodename" not in host:
                host["nodename"] = host["hostip"]

        return kubernetes_config, node_list

    def machine_add(self, args):
        cluster_object_model_k8s, node_list = self.process_args(args)

        for host in node_list["machine-list"]:
            if host["k8s-role"] == "master":
                logger.info("master node can not be added")
                raise Exception("master node can not be added")

        for host in node_list["machine-list"]:
            add_worker = k8s_add.add(cluster_object_model_k8s, host, True)
            add_worker.run()



    def machine_remove(self, args):
        cluster_object_model_k8s, node_list = self.process_args(args)

        for host in node_list["machine-list"]:
            if host["k8s-role"] == "master":
                logger.info("master node can not be removed")
                raise Exception("master node can not be removed")

        for host in node_list["machine-list"]:
            add_worker = k8s_remove.remove(cluster_object_model_k8s, host, True)
            add_worker.run()


